package com.syncnews.base;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public abstract class BaseDAO {
	protected Connection conn;
	protected Statement stmt;
	protected PreparedStatement pStmt;
	protected ResultSet rs;
	protected CallableStatement cStmt;

	public static String loadValue(String key) throws IOException {
		Properties properties = new Properties();
		properties.load(new FileInputStream("config.properties"));
		String userAgent = properties.getProperty(key);
		return userAgent;
	}

	/**
	 * Get conection with url , username , password
	 * @param url
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public Connection getConnection(String classForname,String url,String username,String password) throws Exception {
		try {
			Class.forName(classForname);
			this.conn = DriverManager.getConnection(url,username, password);
			return this.conn;
		} catch (Exception e) {
			throw e;
		}
	}

	public void close() {
		try {
			if (this.rs != null) {
				this.rs.close();
			}
			if (this.stmt != null) {
				this.stmt.close();
			}
			if (this.pStmt != null) {
				this.pStmt.close();
			}
			if (this.cStmt != null) {
				this.cStmt.close();
			}
			if (this.conn != null)
				this.conn.close();
		} catch (Exception e) {
			this.rs = null;
			this.stmt = null;
			this.pStmt = null;
			this.cStmt = null;
			this.conn = null;
		}
	}
}
